// StorageDatabase.cpp: implementation of the CStorageDatabase class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "StorageDatabase.h"

//////////////////////////////////////////////////////////////////////
// Interface
//////////////////////////////////////////////////////////////////////

void CStorageDatabase::SetConnSql(const CString& strSql)
{
	m_strSql = strSql;
}

CADORecordset* CStorageDatabase::Connect()
{
	if (NULL != m_pRS)
		return( m_pRS );

	if (m_strSql.IsEmpty())
		return( NULL );
	if (false == _ConnectDB())
		return( NULL );
	CADORecordset* pRS = new CADORecordset(&m_Ado);
	if (NULL == pRS)
		return( NULL );
	if (false == pRS->Open( (LPCTSTR)m_strSql ))
	{
		delete pRS;
		ASSERT( FALSE );
		return( NULL );
	}

	m_pRS = pRS;
	return( m_pRS );
}

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CADODatabase CStorageDatabase::m_Ado;
CString CStorageDatabase::m_strConnect;

CStorageDatabase::CStorageDatabase()
	: m_pRS(NULL)
{
}

CStorageDatabase::~CStorageDatabase()
{
	if ((NULL != m_pRS)
	 && (m_pRS->IsOpen()))
	{
		VERIFY( m_pRS->Update() );
	}
}

//////////////////////////////////////////////////////////////////////
// Implement
//////////////////////////////////////////////////////////////////////

void CStorageDatabase::_ReadConfiguration()
{
	if (m_strConnect.IsEmpty())
	{
		CString strServer,strUser,strPwd,strDB,strFlag;

		char  pIniValue[100];
		DWORD nRet;
		//config name
		//read store wave file path from config file
		nRet=GetPrivateProfileString ( LPCTSTR("Server"), LPCTSTR("User"), 
			 "", pIniValue, 100, LPCTSTR(m_strConfFilePath) );
		strUser.Format("%s",pIniValue);

		memset(pIniValue,0,100);
		nRet=GetPrivateProfileString ( LPCTSTR("Server"), LPCTSTR("Pwd"), 
			 "", pIniValue, 100, LPCTSTR(m_strConfFilePath) );

		strPwd.Format("%s",pIniValue);
		
		nRet=GetPrivateProfileString ( LPCTSTR("Server"), LPCTSTR("DB"), 
			 "", pIniValue, 100, LPCTSTR(m_strConfFilePath) );

		strDB.Format("%s",pIniValue);

		nRet=GetPrivateProfileString ( LPCTSTR("Server"), LPCTSTR("Flag"), 
			 "", pIniValue, 100, LPCTSTR(m_strConfFilePath) );

		strFlag.Format("%s",pIniValue);

		
		nRet=GetPrivateProfileString ( LPCTSTR("Server"), LPCTSTR("Server"), 
			 "", pIniValue, 100, LPCTSTR(m_strConfFilePath) );
		
		strServer.Format("%s",pIniValue);

		if(strFlag=="0")
			m_strConnect.Format("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=%s;Data Source=%s",strDB,strServer);
		else
			m_strConnect.Format("Provider=SQLOLEDB.1;Password=%s;Persist Security Info=True;User ID=%s;Initial Catalog=%s;Data Source=%s",strPwd,strUser,strDB,strServer);
	}
}

bool CStorageDatabase::_ConnectDB()
{
	if (m_Ado.IsOpen())
		return( true );

	if (m_strConnect.IsEmpty())
		_ReadConfiguration();
	ASSERT(FALSE == m_strConnect.IsEmpty());

	return( m_Ado.Open( LPCTSTR(m_strConnect) ) );
}

CADORecordset* CStorageDatabase::PreSave()
{
	CADORecordset* pRS= Connect();
	if (NULL == pRS)
		return( NULL );

	if (pRS->GetRecordCount() <= 0)
	{
		pRS->AddNew();
		OnNew( pRS );
	}
	else
	{
		pRS->Edit();
	}

	return( pRS );
}

void CStorageDatabase::Remove()
{
	CADORecordset* pRS= Connect();
	if (pRS->GetRecordCount() > 0)
		VERIFY( pRS->Delete() );
}
